import numpy as np
import pandas as pd
import pyecharts

class GetBar:

    def __init__(self, csv_path):
        self.df = pd.read_csv(csv_path, sep='\t')

    def get_bar(self, series_info, bar_name, name):
        bar = pyecharts.Bar(bar_name,width=1500,height=700)
        asix0 = series_info.index
        asix1 = series_info.values.ravel()
        bar.add(name, asix0, asix1, is_label_show=True, is_datazoom_show=True, is_yaxis_inverse=True)
        return bar

    # 每件卖多少
    def how_many_are_sold_per_item(self):
        info = self.df.groupby('item_name').sum().drop('order_id', axis=1)
        return info

    def bar_how_many_are_sold_per_item(self, bar_name='test', name='test_name'):
        return self.get_bar(self.how_many_are_sold_per_item(), bar_name, name)

    # 每件卖多少钱
    def how_much_are_sold_per_item(self):
        self.df['total_prices'] = self.df.quantity*self.df.item_price.str.replace('$','').astype('float64')
        info = round(self.df.groupby('item_name').sum().drop('order_id', axis=1).drop('quantity', axis=1), 2)
        return info

    def bar_how_much_are_sold_per_item(self, bar_name='test', name='test_name'):
        return self.get_bar(self.how_much_are_sold_per_item(), bar_name, name)


